package ct

import (
	"encoding/json"
	"log"
	"server/global"
)

type T struct {
	Type  string `json:"type"`
	Field string `json:"field,omitempty"`
	Title string `json:"title,omitempty"`
	Info  string `json:"info,omitempty"`
}

// NewTable 创建新的数据表
// name string 表明
// notes string 表备注
func NewTable(name, notes, form string, isVote int) error {

	sql := "CREATE TABLE " + name
	sql += " ( "
	sql += "`id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',"
	sql += "`create_time` int NULL DEFAULT NULL COMMENT '创建时间',"
	sql += "`update_time` int NULL DEFAULT NULL COMMENT '修改时间',"
	sql += "`user_id` int NULL DEFAULT NULL COMMENT '用户id',"

	//开启投票 增加投票统计字段
	if isVote == 1 {
		sql += "`vote_num` int NULL DEFAULT 0 COMMENT '票数',"
	}

	sql += "`form` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '表单'"

	var m []T

	if err := json.Unmarshal([]byte(form), &m); err != nil {
		log.Println(name+" json解析错误：", err)
		return err
	}

	for _, v := range m {

		if v.Field != "" {
			switch v.Type {

			case "div", "el-divider", "FcRow", "span", "el-button", "el-alert":

			case "datePicker":
				sql += ",`" + v.Field + "` date NULL DEFAULT NULL COMMENT '" + v.Title + "'"

			case "DateTime":
				sql += ",`" + v.Field + "` datetime NULL DEFAULT NULL COMMENT '" + v.Title + "'"

			case "inputNumber", "slider":
				sql += ",`" + v.Field + "` int NULL DEFAULT 0 COMMENT '" + v.Title + "'"

			case "fc-editor":
				sql += ",`" + v.Field + "` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL COMMENT '" + v.Title + "'"

			case "timePicker":
				sql += ",`" + v.Field + "` time NULL DEFAULT NULL COMMENT '" + v.Title + "'"

			default:
				sql += ",`" + v.Field + "` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '" + v.Title + "'"
			}
		}

	}

	sql += ",PRIMARY KEY (`id`) USING BTREE"
	sql += " ) "
	sql += "ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '" + notes + "' ROW_FORMAT = DYNAMIC;"

	Db := global.GL_DB

	if err := Db.Exec(sql).Error; err != nil {
		log.Println(name+" 数据表生成错误：", err)
		return err
	}

	return nil

}

// NewCollectVoteTable 创建征集投票统计表
// name string 表明
// notes string 表备注
func NewCollectVoteTable(name, notes string) error {

	sql := "CREATE TABLE " + name
	sql += " ( "
	sql += "`id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',"
	sql += "`create_time` int NULL DEFAULT NULL COMMENT '创建时间',"
	sql += "`user_id` int NULL DEFAULT NULL COMMENT '用户id',"
	sql += "`collect_data_id` int NULL DEFAULT NULL COMMENT '数据表id',"
	sql += "`collect_id` int NULL DEFAULT NULL COMMENT '活动id',"
	sql += "PRIMARY KEY (`id`) USING BTREE"
	sql += " ) "
	sql += "ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '" + notes + "' ROW_FORMAT = DYNAMIC;"

	Db := global.GL_DB

	if err := Db.Exec(sql).Error; err != nil {
		log.Println(name+" 数据表生成错误：", err)
		return err
	}

	return nil

}

// NewVoteTable 创建投票统计表
// name string 表明
// notes string 表备注
func NewVoteTable(name, notes string) error {

	sql := "CREATE TABLE " + name
	sql += " ( "
	sql += "`id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',"
	sql += "`create_time` int NULL DEFAULT NULL COMMENT '创建时间',"
	sql += "`user_id` int NULL DEFAULT NULL COMMENT '用户id',"
	sql += "`vote_data_id` int NULL DEFAULT NULL COMMENT '数据表id',"
	sql += "`vote_id` int NULL DEFAULT NULL COMMENT '活动id',"
	sql += "PRIMARY KEY (`id`) USING BTREE"
	sql += " ) "
	sql += "ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '" + notes + "' ROW_FORMAT = DYNAMIC;"

	Db := global.GL_DB

	if err := Db.Exec(sql).Error; err != nil {
		log.Println(name+" 数据表生成错误：", err)
		return err
	}

	return nil

}
